home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 21 code / Hierarchical Lists / CMyCustomListBox.cp < prev    next >
Encoding:
Text File  |  1994-09-15  |  2.6 KB  |  95 lines  |  [TEXT/MMCC]

  1. // ===========================================================================
  2. //    CMyCustomListBox.cp            ©1994 Jan Bruyndonckx All rights reserved.
  3. //    v1.0 18/6/94
  4. //
  5. //    A listbox subclass containing an icon and a p-string
  6. // ===========================================================================
  7.  
  8. #include <string.h>
  9. #include "CMyCustomListBox.h"
  10.  
  11. typedef struct {
  12.  
  13.     short    iconID ;
  14.     Str255    name ;        // actually, we'll use only as much as needed…
  15.     
  16. } MyCustomDataRec, *MyCustomDataRecPtr ;
  17.  
  18. typedef struct {
  19.  
  20.     short        iconID ;
  21.     StringPtr    name ;
  22.     
  23. } MyCustomData ;
  24.  
  25. MyCustomData    sMyCustomData[] = {
  26.  
  27.     -4000,    "\pdocument",
  28.     -3999,    "\pfolder",
  29.     -3998,    "\pfloppy",
  30.     -3996,    "\papplication",
  31.     -3995,    "\phard disk",
  32.     -3993,    "\ptrash",
  33.     -3985,    "\pstationary",
  34.     -3972,    "\pserver",
  35.     -3970,    "\psuitcase"
  36. } ;
  37.  
  38. //----------------------------------------------------------------------------
  39.  
  40. CMyCustomListBox* CMyCustomListBox::CreateFromStream (LStream *inStream)
  41. {
  42.   return (new CMyCustomListBox(inStream));
  43. }
  44.  
  45. //----------------------------------------------------------------------------
  46.  
  47. CMyCustomListBox::CMyCustomListBox (LStream *inStream) : CCustomListBox (inStream)
  48. { const short numCells = sizeof (sMyCustomData) / sizeof (MyCustomData) ;
  49.   Cell  cell = { 0, 0 } ;
  50.   short &i   = cell.v ;
  51.   
  52.   ::LAddRow (numCells, 0, mMacListH) ;
  53.  
  54.   for (cell.v = 0 ; cell.v < numCells ; cell.v++)
  55.       { MyCustomDataRec    cellData ;
  56.         cellData.iconID = sMyCustomData[i].iconID ;
  57.         ::memcpy (cellData.name, sMyCustomData[i].name, *sMyCustomData[i].name+1) ;
  58.         ::LSetCell (&cellData, sizeof (short) + *cellData.name + 1, cell, mMacListH) ;
  59.       }
  60. }
  61.  
  62. //----------------------------------------------------------------------------
  63.  
  64. void CMyCustomListBox::DrawElementSelf (const Rect *lRect, 
  65.                                         const void *lElement, 
  66.                                         const short lDataLen)
  67. { Rect                    sicnBox ;
  68.   MyCustomDataRecPtr    cellData = (MyCustomDataRecPtr) lElement ;
  69.  
  70.   sicnBox.left = lRect->left + 3 ;
  71.   sicnBox.top  = lRect->top - 1 ;
  72.   sicnBox.right  = sicnBox.left + 16 ;
  73.   sicnBox.bottom = sicnBox.top + 16 ;
  74.   
  75.   Handle h = ::GetResource ('SICN', cellData->iconID) ;
  76.   if (h != NULL)
  77.     { char saveState = ::HGetState (h) ;
  78.       ::HLock(h) ;
  79.       
  80.       BitMap    srcBits = { *h, 2, { 0, 0, 16, 16 }};    // baseAddr, rowBytes, bounds
  81.       GrafPtr    port ;
  82.       
  83.       ::GetPort (&port) ;
  84.       ::CopyBits (&srcBits, &(*port).portBits, 
  85.                     &srcBits.bounds, &sicnBox, 
  86.                     srcCopy, NULL) ;
  87.  
  88.       ::HSetState (h, saveState) ;
  89.       }
  90.   ::MoveTo (lRect->left + 24, lRect->top + 10) ;
  91.   ::DrawString (cellData->name) ;
  92. }
  93.  
  94. //----------------------------------------------------------------------------
  95.